package petrus.components.dropdown 
{
	import adobe.utils.CustomActions;
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import pages.utils.components.AbstractComponent;
	import pages.utils.components.scroll.AbstractScrollArea;
	import pages.utils.components.scroll.IScrollArea;
	
	/**
	 * ...
	 * @author Benoit Petrus
	 */
	public class AbstractDropDown  extends AbstractComponent implements IDropDown
	{
		private var scrollArea:AbstractScrollArea;
		private var isOpen : Boolean = false;
		public var button:MovieClip;
		
		public function AbstractDropDown() 
		{
			super()			
		}
		
		
		
		public function onItemChoosed(itemLabel:String):void 
		{			
			updateText(itemLabel)
			close()
		}		
		
		public function setArea(p_scrollArea:AbstractScrollArea):void 
		{
			scrollArea = p_scrollArea;
			scrollArea.x = button.x
			scrollArea.y = button.y + button.height ;
			
			addChild(scrollArea);
			
			isOpen = false
			TweenMax.to(scrollArea, 0, {autoAlpha:0});
		}
		
		public function updateText(label:String):void 
		{
			button.tf.text = label;
		}
		
		public function setButton(mc : MovieClip):void 
		{
			trace("[AbstractDropDown.setButton] ");
			button = mc ;			
			button.mouseChildren = false
			button.buttonMode = true			
			button.addEventListener(MouseEvent.CLICK , toggleVisibility)
			addChild(button);
		}
		
		public function reset(label:String):void 
		{
			updateText(label)
			close()
		}
		
		private function toggleVisibility(e:MouseEvent):void 
		{
			if (isOpen) close()
			else open()
			
			
			trace("[AbstractDropDown.toggleVisibility] "+isOpen);
		}
		
		private function open():void 
		{
			isOpen = true
			//scrollArea.visible = true
			TweenMax.to(scrollArea, .2, {autoAlpha:1});
		}
		
		private function close():void 
		{ 
			isOpen = false
			//scrollArea.visible = false
			TweenMax.to(scrollArea, .2, {autoAlpha:0});
		}

		
	}

}